package com.example.commuity.adapter.provider

import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.commuity.R
import com.example.commuity.adapter.ItemSquareCardAdapter
import com.example.commuity.adapter.RecommendItemType
import com.example.commuity.databinding.ItemRecommendSquareViewBinding
import com.example.commuity.entity.HorizontalScrollCardEntity
import com.example.commuity.entity.SquareCard
import com.example.entity.BaseEntity
import com.example.recyclerview.RecyclerItemDecoration

/**
 * 社区推荐卡片
 * */
class SquareCardViewProvider :BaseItemProvider<BaseEntity>(){


    private lateinit var itemSquareCardAdapter: ItemSquareCardAdapter

    override val itemViewType: Int=RecommendItemType.SQUARE_CARD_VIEW
    override val layoutId: Int= R.layout.item_recommend_square_view

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        var dataBinding=DataBindingUtil.bind<ItemRecommendSquareViewBinding>(viewHolder.itemView)
        var layoutParams = StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,SizeUtils.dp2px(80f))
        layoutParams.isFullSpan=true
        viewHolder.itemView.layoutParams=layoutParams


        dataBinding?.rvSquare?.setHasFixedSize(true)
        dataBinding?.rvSquare?.layoutManager=GridLayoutManager(viewHolder.itemView.context,1,GridLayoutManager.HORIZONTAL,false)
        dataBinding?.rvSquare?.addItemDecoration(RecyclerItemDecoration(0, 0,0, SizeUtils.dp2px(5f)))

    }

    override fun convert(helper: BaseViewHolder, item: BaseEntity) {

        var dataBinding=DataBindingUtil.getBinding<ItemRecommendSquareViewBinding>(helper.itemView)

        if (item!=null && dataBinding!=null){
            var entity =item as HorizontalScrollCardEntity
             itemSquareCardAdapter=ItemSquareCardAdapter(R.layout.item_recommend_sqare_item_view)
            dataBinding.rvSquare.adapter=itemSquareCardAdapter
            itemSquareCardAdapter.setNewInstance(entity.data.itemList as MutableList<SquareCard>)
            dataBinding.executePendingBindings()


        }

    }
}